home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / AX25ROUT.C < prev    next >
Text File  |  1993-10-05  |  55KB  |  2,258 lines

  1. /* Low level AX.25 frame processing - address header and routing */
  2. /* WAMPES parts by DK5SG modified and ported to WNOS by DB3FL */
  3. /* DAMA parts by DL1BKE modified and ported to WNOS by DB3FL */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "global.h"
  8. #include "config.h"
  9. #ifdef AX25
  10. #include "socket.h"
  11. #include "mbuf.h"
  12. #include "iface.h"
  13. #include "timer.h"
  14. #include "arp.h"
  15. #include "slip.h"
  16. #include "ax25.h"
  17. #ifdef NETROM
  18. #include "netrom.h"
  19. #endif
  20. #include "ip.h"
  21. #include "tcp.h"
  22. #include "trace.h"
  23. #include "files.h"
  24. #include "icmp.h"
  25.  
  26. #define axptr(a)        ((struct ax25_addr *) (a))
  27. #define next_seq(n)     (((n) + 1) & MMASK)
  28. #define T1_TIME                 3000
  29.  
  30. char Mycall[AXALEN] = "\0";
  31. char Nomycall[] = "Mycall not set\n";
  32.  
  33. static int MHwait = 0;       /* Semaphore to serialize access to heardlist */
  34.  
  35. #ifdef NETROM
  36. extern void nr_derate __ARGS((struct ax25_cb *axp));
  37. #endif
  38.  
  39. struct ax25_cb *Ax25_cb = NULLAX25;
  40. struct iface *axroute_default_ifp = NULLIF;
  41. struct axroute_tab *Axroute_tab = 0;
  42.  
  43. int t1_timeout __ARGS((struct ax25_cb *cp));
  44.  
  45. static void near axroute __ARGS((struct ax25_cb *axp,struct mbuf *bp));
  46. static void near axproto_recv __ARGS((struct iface *ifp,struct mbuf *bp,char repeated));
  47. static void near setaxstate __ARGS((struct ax25_cb *axp,int newstate));
  48. static void near logaddr __ARGS((struct iface *iface,char *addr,char pid));
  49.  
  50. /* Default AX.25 parameters */
  51. int16 T1init =          10;             /* Retransmission timeout */
  52. int16 T2init =          2;              /* Acknowledgement delay timeout */
  53. int16 T3init =          600;    /* keep-alive polling */
  54. int16 T4init =          60;             /* Busy timeout */
  55. int16 T5init =      1;          /* Packet assembly timeout */
  56. int16 Maxframe =        2;              /* Stop and wait */
  57. int16 Retries =         10;             /* 10 retries */
  58. int16 Axwindow =        2048;   /* 2K incoming text before RNR'ing */
  59. int16 Paclen =          256;    /* 256-byte I fields */
  60. int16 Pthresh =         64;             /* Send polls for packets larger than this */
  61. int16 Digipeat =        2;              /* Controls digipeating */
  62. int   T3disc =          1;              /* 0 = polling, 1 = disconnecting */
  63.  
  64. /* List of AX.25 multicast addresses in network format (shifted ascii).
  65.  * Only the first entry is used for transmission, but an incoming
  66.  * packet with any one of these destination addresses is recognized
  67.  * as a multicast.
  68.  * DON'T CHANGE THE SEQUENCE !!!!!!!!!
  69.  */
  70. char Ax25multi[][AXALEN] = {
  71.     'Q'<<1, 'S'<<1, 'T'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* QST */
  72.     'N'<<1, 'O'<<1, 'D'<<1, 'E'<<1, 'S'<<1, ' '<<1, '0'<<1, /* NODES */
  73.     'I'<<1, 'D'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* ID */
  74.     'M'<<1, 'A'<<1, 'I'<<1, 'L'<<1, ' '<<1, ' '<<1, '0'<<1, /* MAIL */
  75.     'O'<<1, 'P'<<1, 'E'<<1, 'N'<<1, ' '<<1, ' '<<1, '0'<<1, /* OPEN */
  76.     'C'<<1, 'Q'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* CQ */
  77.     'B'<<1, 'E'<<1, 'A'<<1, 'C'<<1, 'O'<<1, 'N'<<1, '0'<<1, /* BEACON */
  78.     'R'<<1, 'M'<<1, 'N'<<1, 'C'<<1, ' '<<1, ' '<<1, '0'<<1, /* RMNC */
  79.     'A'<<1, 'L'<<1, 'L'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* ALL */
  80.     'F'<<1, 'L'<<1, 'X'<<1, 'N'<<1, 'E'<<1, 'T'<<1, '0'<<1, /* FLXNET */
  81.     'L'<<1, '3'<<1, 'R'<<1, 'T'<<1, 'T'<<1, ' '<<1, '0'<<1, /* L3RTT  */ /* bke 920712 */
  82.     '\0',
  83. };
  84.  
  85. /* New-style frame segmenter. Returns queue of segmented fragments, or
  86.  * original packet if small enough
  87.  */
  88. #ifdef NETROM
  89. struct mbuf *
  90. #else
  91. static struct mbuf *
  92. #endif
  93. segmenter(
  94. struct mbuf *bp,        /* Complete packet */
  95. int16 ssize)            /* Max size of frame segments */
  96. {
  97.     struct mbuf *bp1, *bptmp, *result = NULLBUF;
  98.     int16 len = len_p(bp), offset = 0;
  99.     int segments;
  100.  
  101.     /* See if packet is too small to segment. Note 1-byte grace factor
  102.      * so the PID will not cause segmentation of a 256-byte IP datagram.
  103.      */
  104.     if(len <= ssize + 1) {
  105.         /* Too small to segment */
  106.         return bp;
  107.     }
  108.     ssize -= 2;                                                     /* ssize now equal to data portion size */
  109.     segments = 1 + (len - 1) / ssize;       /* # segments  */
  110.  
  111.     while(segments != 0) {
  112.         offset += dup_p(&bptmp,bp,offset,ssize);
  113.         if(bptmp == NULLBUF) {
  114.             free_q(&result);
  115.             break;
  116.         }
  117.         /* Make room for segmentation header */
  118.         bp1 = pushdown(bptmp,2);
  119.         bp1->data[0] = PID_SEGMENT;
  120.         bp1->data[1] = --segments;
  121.         if(offset == ssize)
  122.             bp1->data[1] |= SEG_FIRST;
  123.         enqueue(&result,bp1);
  124.     }
  125.     free_p(bp);
  126.     return result;
  127. }
  128.  
  129. /* Send IP datagrams across an AX.25 link */
  130. int
  131. ax_send(struct mbuf *bp,struct iface *iface,int32 gateway,int prec,int del,int tput,int rel)
  132. {
  133.     char *hw_addr;
  134.     struct ax25_cb *axp, *axp1;
  135.     struct mbuf *tbp;
  136.     int16 mode;
  137.     struct ip bp1;
  138.     struct arp_tab *arp;
  139.     struct connection conn;
  140.     struct tcp seg;
  141.     struct tcb *tcb;
  142.  
  143.     if(gateway == iface->broadcast) /* This is a broadcast IP datagram */
  144.          return (*iface->output)(iface,Ax25multi[0],iface->hwaddr,PID_IP,bp);
  145.  
  146.     if((arp = arp_lookup(ARP_AX25,gateway)) != NULLARP && arp->state == ARP_VALID) {
  147.         hw_addr = arp->hw_addr;
  148.         mode = arp->flags;
  149.     } else if((hw_addr = res_arp(iface,ARP_AX25,gateway,bp)) == NULLCHAR) {
  150.         return 0;               /* Wait for address resolution */
  151.     }
  152.     if(del || (!rel && (mode == DATAGRAM_MODE)) || addreq(hw_addr,Ax25multi[0])) {
  153.         mode = DATAGRAM_MODE;
  154.     }
  155.     if(mode == DATAGRAM_MODE) {
  156.         /* Use UI frame */
  157.         return (*iface->output)(iface,hw_addr,iface->hwaddr,PID_IP,bp);
  158.     }
  159.     if((axp = find_ax25(hw_addr,iface->hwaddr)) == NULLAX25) {
  160.         /* Open a new connection */
  161.         if((axp1 = open_ax25(iface,iface->hwaddr,hw_addr,
  162.           AX_ACTIVE,s_arcall,s_atcall,s_ascall,-2)) == NULLAX25) {
  163.             goto quit;
  164.         }
  165.         axp1->mode = DGRAM;
  166.     }
  167.  
  168.     if(axp == NULLAX25) {
  169.         axp = axp1;
  170.         axp->user = 0;
  171.         if(axp->state == DISCONNECTED) {
  172.             setaxstate(axp,CONNECTING);
  173.         }
  174.     }
  175.     dup_p(&tbp,bp,0,len_p(bp));
  176.  
  177.     if (ntohip(&bp1,&tbp) != -1) {
  178.       if (ntohtcp(&seg,&tbp) != -1) {
  179.         conn.local.port = seg.source;
  180.         conn.local.address = bp1.source;
  181.         conn.remote.port = seg.dest;
  182.         conn.remote.address = bp1.dest;
  183.  
  184.         if((tcb = lookup_tcb(&conn)) != NULLTCB && len_p(tbp) > 0
  185.           && tcb->state != TCP_TIME_WAIT) {
  186.             set_timer(&tcb->timer,10 * dur_timer(&tcb->timer));
  187.             if(dur_timer(&tcb->timer) > 300000L)
  188.                 set_timer(&tcb->timer,300000L);
  189.             start_timer(&tcb->timer);
  190.         }
  191.       }
  192.     }
  193.     free_p(tbp);
  194.  
  195.     /* discard IP frames and send a SOURCE_QUENCH if there already are bytes
  196.      * in the txq - DC0HK.920401 */
  197.     if(len_p(axp->txq) > axp->iface->mtu) {
  198.         ntohip(&bp1,&bp);
  199.         icmp_output(&bp1,bp,ICMP_QUENCH,0,NULL);
  200.         goto quit;
  201.     }
  202.     tbp = pushdown(bp,1);
  203.  
  204.     /* Insert the PID */
  205.     tbp->data[0] = (mode == IPCAM_MODE) ? PID_NO_L3 : PID_IP;
  206.  
  207.     /* Look for segmentation */
  208.     if((bp = segmenter(tbp,axp->iface->flags->paclen)) != NULLBUF) {
  209.         send_ax25(axp,bp,DGRAM);
  210.         return 0;
  211.     }
  212. quit:
  213.     free_p(bp);
  214.     return -1;
  215. }
  216.  
  217. /* Add header and send connectionless (UI) AX.25 packet.
  218.  * Note that the calling order here must match enet_output
  219.  * since ARP also uses it. */
  220. int
  221. ax_output(
  222. struct iface *iface,    /* Interface to use; overrides routing table */
  223. char *dest,             /* Destination AX.25 address (7 bytes, shifted) */
  224. char *source,           /* Source AX.25 address (7 bytes, shifted) */
  225. int16 pid,              /* Protocol ID */
  226. struct mbuf *data)      /* Data field (follows PID) */
  227. {
  228.     struct mbuf *abp;
  229.     struct ax25_cb axp;
  230.     char path[3*AXALEN], *cp;
  231.  
  232.     memcpy(path,dest,AXALEN);
  233.     path[ALEN] &= ~E;
  234.  
  235.     memcpy(path + AXALEN,source,AXALEN);
  236.     path[ALEN + AXALEN] |= E;
  237.  
  238.     addrcp(axp.path + AXALEN,axp.iface->hwaddr);
  239.     build_path(&axp,NULLIF,path,0);
  240.  
  241.     axp.iface = iface;
  242.     axp.path[ALEN + AXALEN] &= ~C;
  243.  
  244.     /* Allocate mbuf for control and PID fields, and fill in */
  245.     abp = pushdown(data,axp.pathlen + 2);
  246.     memcpy(abp->data,axp.path,axp.pathlen);
  247.     cp = abp->data + axp.pathlen;
  248.     *cp++ = UI;
  249.     *cp = pid;
  250.  
  251.     axroute(&axp,abp);
  252.     return 0;
  253. }
  254.  
  255. struct axroute_tab *
  256. axroute_tabptr(struct ax25_addr *call,int create)
  257. {
  258.   struct axroute_tab *rp = 0;
  259.  
  260.   for(rp = Axroute_tab;
  261.     rp && !addreq((char *)&rp->call,(char *)call);
  262.     rp = rp->next) ;
  263.  
  264.   if(!rp && create) {
  265.     rp = mxallocw(sizeof(struct axroute_tab));
  266.     rp->call = *call;
  267.  
  268.     if(Axroute_tab == 0 || strcmp((char *)&rp->call,(char *)&Axroute_tab->call) < 0) {
  269.       rp->next = Axroute_tab;
  270.       Axroute_tab = rp;
  271.     } else {
  272.       struct axroute_tab *rrp, *rptmp;
  273.  
  274.       for(rrp = Axroute_tab; rrp; rrp = rrp->next) {
  275.         if(strcmp((char *)&rp->call,(char *)&rrp->call) > 0) {
  276.           if(rrp->next == (struct axroute_tab *)0) {
  277.             rrp->next = rp;
  278.             rp->next = (struct axroute_tab *)0;
  279.             break;
  280.             }
  281.           rptmp = rrp;
  282.           continue;
  283.         }
  284.         rptmp->next = rp;
  285.         rp->next = rrp;
  286.         break;
  287.       }
  288.     }
  289.   }
  290.   return rp;
  291. }
  292.  
  293. void
  294. axroute_add(struct ax25_cb *cp,int perm)
  295. {
  296.   int i, ncalls = 0;
  297.   char  *ap;
  298.   struct axroute_tab *rp, *lastnode = 0;
  299.   struct ax25_addr calls[MAXDIGIS + 1];
  300.  
  301.   for (ap = cp->path + AXALEN; !addreq(ap,cp->iface->hwaddr); ap += AXALEN) ;
  302.  
  303.   do {
  304.     ap += AXALEN;
  305.     if (ap >= cp->path + cp->pathlen) {
  306.       ap = cp->path;
  307.     }
  308.     if (!*ap || addreq(ap,cp->iface->hwaddr)) {
  309.       return;
  310.     }
  311.     for (i = 0; i < ncalls; i++) {
  312.       if (addreq((char *) (calls + i), ap)) {
  313.         return;
  314.       }
  315.     }
  316.     calls[ncalls++] = *axptr(ap);
  317.   } while (ap != cp->path);
  318.  
  319.   for (i = 0; i < ncalls; i++) {
  320.     rp = axroute_tabptr(calls + i, 1);
  321.     if (perm || !rp->perm) {
  322.       if (lastnode) {
  323.     rp->digi = lastnode;
  324.     rp->ifp = 0;
  325.       } else {
  326.     rp->digi = 0;
  327.         rp->ifp = cp->iface;
  328.       }
  329.       rp->perm = perm;
  330.     }
  331.     rp->time = currtime;
  332.     lastnode = rp;
  333.   }
  334. }
  335.  
  336. static void near
  337. axroute(struct ax25_cb *cp,struct mbuf *bp)
  338. {
  339.   char *dest;
  340.   struct axroute_tab *rp;
  341.   struct iface *ifp;
  342.  
  343.   if (cp && cp->iface) {
  344.     ifp = cp->iface;
  345.   } else {
  346.     if (bp->data[AXALEN + ALEN] & E) {
  347.       dest = bp->data;
  348.     } else {
  349.       for (dest = bp->data + 2 * AXALEN; ; dest += AXALEN) {
  350.         if (!(dest[ALEN] & REPEATED)) {
  351.           break;
  352.         }
  353.     if (dest[ALEN] & E) {
  354.       dest = bp->data;
  355.       break;
  356.     }
  357.       }
  358.     }
  359.     rp = axroute_tabptr(axptr(dest),0);
  360.     ifp = (rp && rp->ifp) ? rp->ifp : axroute_default_ifp;
  361.   }
  362.   if (ifp) {
  363.     if (ifp->forw) {
  364.       ifp = ifp->forw;
  365.     }
  366.     ifp->rawsndcnt++;
  367.     ifp->lastsent = secclock();
  368.     (*ifp->raw)(ifp, bp);
  369.   } else {
  370.     free_p(bp);
  371.   }
  372.   return;
  373. }
  374.  
  375. static void near
  376. send_packet(struct ax25_cb *cp,int type,int cmdrsp,struct mbuf *data)
  377. {
  378.   int  control = type;
  379.   struct mbuf *bp = alloc_mbuf(cp->pathlen + 1);
  380.   char *p = bp->data;
  381.  
  382.   memcpy(p, cp->path, cp->pathlen);
  383.   if (cmdrsp & DST_C) p[ALEN] |= C;
  384.   if (cmdrsp & SRC_C) p[ALEN + AXALEN] |= C;
  385.   p += cp->pathlen;
  386.  
  387.   if (type == I) {
  388.     control |= (cp->vs << 1);
  389.     cp->vs = next_seq(cp->vs);
  390.   }
  391.   if ((type & 3) != U) {
  392.     control |= (cp->vr << 5);
  393.     stop_timer(&cp->t2);
  394.   }
  395.   if (cmdrsp & PF) {
  396.     control |= PF;
  397.   }
  398.   *p++ = control;
  399.  
  400.   if (type == RR || type == REJ || type == UA) {
  401.     cp->rnrsent = 0;
  402.   }
  403.   if (type == RNR) {
  404.     cp->rnrsent = 1;
  405.   }
  406.   if (type == REJ) {
  407.     cp->rejsent = 1;
  408.   }
  409.   if (cmdrsp == POLL) {
  410.     cp->polling = 1;
  411.   }
  412.   if (type == I || cmdrsp == POLL) {
  413.     start_timer(&cp->t1);
  414.   }
  415.   if (type == I) {
  416.     start_timer(&cp->t3);
  417.   }
  418.   bp->cnt = p - bp->data;
  419.   bp->next = data;
  420.  
  421.   axroute(cp, bp);
  422. }
  423.  
  424. void
  425. ax_recv(struct iface *iface,struct mbuf *bp)
  426. {
  427.   char axheader[10*AXALEN+1], (*mpp)[AXALEN], *ap, *cntrlptr;
  428.   int addrsize, pid, multicast = 0;
  429.   struct ax25_cb axp;
  430.  
  431.   if (!(bp && bp->data)) goto discard;
  432.   if (is_bud(bp->data + AXALEN,0)) goto discard;
  433.   for (cntrlptr = bp->data; !(*cntrlptr++ & E); ) ;
  434.   addrsize = (int)(cntrlptr - bp->data);
  435.   if(addrsize <  2 * AXALEN
  436.    || addrsize >= bp->cnt
  437.    || addrsize > 10 * AXALEN
  438.    || addrsize % AXALEN) goto discard;
  439.  
  440.   if(addrsize == 2 * AXALEN) {
  441.     logaddr(iface,bp->data + AXALEN,*cntrlptr + 1);
  442.   } else {
  443.     for(ap = bp->data + 2 * AXALEN; ap < cntrlptr; ap += AXALEN) {
  444.       if(!(ap[ALEN] & REPEATED)) {
  445.         logaddr(iface,ap - AXALEN,*cntrlptr + 1);
  446.  
  447.         if(!addreq(ap,iface->hwaddr))
  448.           goto discard;
  449.  
  450.         ap[ALEN] |= REPEATED;
  451.  
  452.         switch(iface->flags->digipeat) {
  453.           case 2:
  454.             axproto_recv(iface,bp,0);
  455.             return;
  456.           case 1:
  457.             axroute(NULLAX25,bp);
  458.             return;
  459.           default:
  460.             goto discard;
  461.         }
  462.       }
  463.     }
  464.     logaddr(iface,ap - AXALEN,*cntrlptr + 1);
  465.   }
  466.  
  467.   if((*cntrlptr & ~PF) != UI) {
  468. /* NREX bke 920716 */
  469. #ifdef NETROM
  470.     if(ismyNcall(bp->data)) {
  471.         axproto_recv(iface,bp,1);
  472.         return;
  473.     }
  474. #endif
  475. /* bke */
  476.     if(!addreq(bp->data,iface->hwaddr)
  477.       && (find_ax25(bp->data + AXALEN,bp->data) == NULLAX25))
  478.         goto discard;
  479.     axproto_recv(iface,bp,1);
  480.     return;
  481.   }
  482. /* NREX bke 920716 */
  483. #ifdef NETROM
  484.   if(ismyNcall(bp->data) || addreq(bp->data,iface->hwaddr)) {
  485.     goto jump;
  486.   }
  487. #else
  488. /* bke */
  489.   if(addreq(bp->data,iface->hwaddr)) {
  490.     goto jump;
  491.   }
  492. #endif
  493.  
  494.   /* Examine immediate destination for a multicast address */
  495.   for(mpp = Ax25multi;(*mpp)[0] != '\0';mpp++)
  496.     if(addreq(bp->data,*mpp)) {
  497.       multicast = 1;
  498.       goto jump;
  499.     }
  500.   goto discard;
  501.  
  502. jump:
  503.   pullup(&bp, axheader, addrsize + 1);
  504.   pid = PULLCHAR(&bp);
  505.   if (!bp) return;
  506.  
  507.   if(!multicast)
  508.     build_path(&axp,iface,axheader,REVERSE);
  509.  
  510.   switch (pid) {
  511.   case PID_IP:
  512.     if(bp->cnt >= 20) {
  513.       struct arp_tab *arp;
  514.  
  515.       if((arp = arp_add(            /* src_ipaddr */
  516.        get32(&bp->data[12]),ARP_AX25,axheader + AXALEN,0,0)) != NULLARP) {
  517.         stop_timer(&arp->timer);
  518.         set_timer(&arp->timer,0L);
  519.       }
  520.     }
  521.     ip_route(iface,iface,bp,multicast);
  522.     return;
  523.   case PID_ARP:
  524.     arp_input(iface,bp);
  525.     return;
  526. #ifdef NETROM
  527.   case PID_NETROM:
  528.     nr_nodercv(iface,axheader + AXALEN,bp);
  529.     return;
  530. #endif
  531. #ifdef AX25
  532.   case PID_NO_L3:       /* FLEXNET-Search should now be answered */
  533.     if(!multicast) {
  534.         send_packet(&axp,DM,FINAL,NULLBUF);
  535.     }
  536. #endif
  537.   default:
  538.     goto discard;
  539.   }
  540. discard:
  541.   free_p(bp);
  542. }
  543.  
  544. #ifdef AXIP
  545. static int32 axipaddr[NAX25];   /* table of IP addresses of AX.25 interfaces */
  546.  
  547. /*
  548.  * FCS lookup table as generated by fcsgen.c
  549.  */
  550. static int16 near fcstab[256] = {
  551.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  552.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  553.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  554.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  555.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  556.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  557.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  558.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  559.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  560.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  561.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  562.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  563.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  564.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  565.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  566.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  567.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  568.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  569.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  570.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  571.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  572.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  573.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  574.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  575.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  576.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  577.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  578.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  579.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  580.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  581.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  582.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  583. };
  584.  
  585. /* raw routine for sending AX.25 on top of IP */
  586. static int
  587. axip_raw(
  588. struct iface *iface,    /* Pointer to interface control block */
  589. struct mbuf *bp)        /* Data field */
  590. {
  591.      int16 len = len_p(bp), fcs = 0xffff;
  592.      struct mbuf *bp1;
  593.  
  594.      if(dup_p(&bp1,bp,0,len) != len) {
  595.         free_p(bp);
  596.         return -1;
  597.      }
  598.      while (len--) {                                /* calculate FCS */
  599.         fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp1)) & 0x00ff];
  600.      }
  601.      fcs ^= 0xffff;                                 /* final FCS (is this right?) */
  602.      bp1 = alloc_mbuf(sizeof(int16));
  603.      *bp1->data = fcs & 0xff;
  604.      *(bp1->data+1) = (fcs >> 8) & 0xff;
  605.      bp1->cnt += sizeof(int16);
  606.      append(&bp,bp1);
  607.      return ip_send(INADDR_ANY,axipaddr[iface->dev],AX25_PTCL,0,0,bp,0,0,0);
  608. }
  609.  
  610. /* Handle AX.25 frames received inside IP according to RFC-1226 */
  611. void
  612. axip_input(
  613. struct iface *iface,    /* Input interface */
  614. struct ip *ip,          /* IP header */
  615. struct mbuf *bp,        /* AX.25 frame with FCS */
  616. int rxbroadcast)                /* Accepted for now */
  617. {
  618.     int i;
  619.     struct mbuf *tbp;
  620.     int16 len, f, fcs = 0xffff;
  621.  
  622.     /* Since the AX.25 frame arrived on an interface that does
  623.        not necessarily support AX.25, we have to find a suitable
  624.        AX.25 interface, or drop the packet.
  625.      */
  626.     /* Try to find a matching AX.25 pseudo interface */
  627.     for(i = 0; i < NAX25; ++i) {
  628.         if(axipaddr[i] == ip->source) {
  629.             break;
  630.         }
  631.     }
  632.     if(i == NAX25) {
  633.         /* Here we could still try to pick a real AX.25 interface,
  634.          * but that would mean that we are accepting AX.25 frames
  635.          * from unknown IP hosts, so we'd rather drop it.
  636.          */
  637.         free_p(bp);
  638.         return;
  639.     }
  640.     for(iface = Ifaces; iface != NULLIF; iface = iface->next) {
  641.         if(iface->raw == axip_raw && iface->dev == i) {
  642.             /* found the right AX.25 pseudo interface */
  643.             break;
  644.         }
  645.     }
  646.     if(iface == NULLIF) {
  647.         free_p(bp);
  648.         return;
  649.     }
  650.     iface->rawrecvcnt++;
  651.     iface->lastrecv = secclock();
  652.     len = len_p(bp) - sizeof(int16);
  653.  
  654.     if(dup_p(&tbp,bp,0,len) != len) {
  655.         free_p(bp);
  656.         return;
  657.     }
  658.     while(len--) {
  659.          fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp)) & 0x00ff];
  660.     }
  661.     fcs ^= 0xffff;
  662.     f = PULLCHAR(&bp);
  663.     f |= (PULLCHAR(&bp) << 8);
  664.  
  665.     if(fcs == f) {
  666.          ax_recv(iface,tbp);
  667.     } else {
  668.          free_p(tbp);
  669.     }
  670. }
  671. #endif /* AXIP */
  672.  
  673. static void near
  674. reset_t1(struct ax25_cb *cp)
  675. {
  676.   int32 tmp = 4 * cp->mdev + cp->srt;
  677.  
  678.   if(tmp > T1_TIME * cp->iface->flags->t1init) {
  679.     tmp = T1_TIME * cp->iface->flags->t1init;
  680.   }
  681.   if(tmp < T1_TIME) {
  682.     tmp = T1_TIME;
  683.   }
  684.   set_timer(&cp->t1,tmp);
  685. }
  686.  
  687. static int near
  688. space_ax25(struct ax25_cb *cp)
  689. {
  690.   if(cp) {
  691.     switch (cp->state) {
  692.     case CONNECTING:
  693.     case CONNECTED:
  694.       if(!cp->closed) {
  695.         int cnt = cp->iface->flags->axwindow - len_p(cp->txq);
  696.         if(cnt > 0) {
  697.           return cnt;
  698.         }
  699.       }
  700.     }
  701.   }
  702.   return 0;
  703. }
  704.  
  705. static int near
  706. busy(struct ax25_cb *cp)
  707. {
  708.   if(cp->peer) {
  709.     return (space_ax25(cp->peer) == 0);
  710.   } else {
  711.     return (len_p(cp->rxq) >= cp->iface->flags->axwindow);
  712.   }
  713. }
  714.  
  715. static void near
  716. send_ack(struct ax25_cb *cp,int cmdrsp)
  717. {
  718.   int type;
  719.  
  720.   if(busy(cp)) {
  721.      type = RNR;
  722.   } else if ( /* !cp->rejsent && */
  723.    (cp->reseq[0].bp || cp->reseq[1].bp || cp->reseq[2].bp ||
  724.     cp->reseq[3].bp || cp->reseq[4].bp || cp->reseq[5].bp ||
  725.     cp->reseq[6].bp || cp->reseq[7].bp)) {
  726.      type = REJ;
  727.   } else {
  728.      type = RR;
  729.   }
  730.   send_packet(cp,type,cmdrsp,NULLBUF);
  731. }
  732.  
  733. static void near
  734. try_send(struct ax25_cb *cp,int fill_sndq,int mode)
  735. {
  736.   stop_timer(&cp->t5);
  737.  
  738.   while(cp->unack < cp->cwind) {
  739.     struct mbuf *bp, *tbp;
  740.     int cnt;
  741.  
  742.     if(cp->state != CONNECTED || cp->remotebusy) {
  743.       return;
  744.     }
  745.     if(fill_sndq && cp->t_upcall) {
  746.       if((cnt = space_ax25(cp)) > 0) {
  747.     (*cp->t_upcall)(cp, cnt);
  748.         if (cp->unack >= cp->cwind) {
  749.           return;
  750.         }
  751.       }
  752.     }
  753.     if(!cp->txq) {
  754.       /* DAMA */
  755.       if(cp->dama && cp->rxasm) {
  756.         t1_timeout(cp);
  757.       }
  758.       return;
  759.     }
  760.     if(mode == STREAM) {
  761.       cnt = len_p(cp->txq);
  762.  
  763.       if(cnt < cp->iface->flags->paclen) {
  764.         if (cp->unack) {
  765.           return;
  766.         } else if(!cp->dama) {
  767.           int32 i = cp->sndqtime + cp->iface->flags->t5init * 1000L;
  768.           int32 j = msclock();
  769.  
  770.           if (!cp->peer && i > j) {
  771.             set_timer(&cp->t5,i - j);
  772.             start_timer(&cp->t5);
  773.             return;
  774.           }
  775.         }
  776.       } else {
  777.         cnt = cp->iface->flags->paclen;
  778.       }
  779.       bp = alloc_mbuf(cnt);
  780.       bp->cnt = cnt;
  781.       pullup(&cp->txq,bp->data,bp->cnt);
  782.       tbp = pushdown(bp,1);
  783.       tbp->data[0] = PID_NO_L3;
  784.       bp = tbp;
  785.     } else {
  786.       bp = dequeue(&cp->txq);
  787.     }
  788.     enqueue(&cp->rxasm, bp);
  789.     cp->unack++;
  790.     cp->sndtime[cp->vs] = msclock();
  791.     dup_p(&bp, bp, 0, MAXINT16);
  792.     send_packet(cp, I, CMD, bp);
  793.   }
  794.   return;
  795. }
  796.  
  797. static void near
  798. setaxstate(struct ax25_cb *cp,int  newstate)
  799. {
  800.   int oldstate = cp->state;
  801.  
  802.   if(oldstate == TIMEWAIT) {
  803.     del_ax25(cp);
  804.     return;
  805.   }
  806.   cp->state = newstate;
  807.   cp->polling = 0;
  808.   cp->retries = 0;
  809.   stop_timer(&cp->t1);
  810.   stop_timer(&cp->t2);
  811.   stop_timer(&cp->t4);
  812.   stop_timer(&cp->t5);
  813.  
  814.   if(!cp->dama) {
  815.     reset_t1(cp);
  816.   }
  817.   switch (newstate) {
  818.   case DISCONNECTED:
  819.     if (cp->peer) {
  820.       disc_ax25(cp->peer);
  821.     }
  822.     if (cp->s_upcall) {
  823.       (*cp->s_upcall)(cp, oldstate, newstate);
  824.     }
  825.     if (cp->peer && cp->peer->state == DISCONNECTED) {
  826.       del_ax25(cp->peer);
  827.       cp->peer = cp->peer->peer = NULLAX25;
  828.     }
  829.     break;
  830.   case CONNECTING:
  831.     if (cp->s_upcall) {
  832.       (*cp->s_upcall)(cp, oldstate, newstate);
  833.     }
  834.     send_packet(cp, SABM, POLL, NULLBUF);
  835.     break;
  836.   case CONNECTED:
  837.     if (cp->peer && cp->peer->state == DISCONNECTED) {
  838.       send_packet(cp->peer, UA, FINAL, NULLBUF);
  839.       setaxstate(cp->peer, CONNECTED);
  840.     }
  841.     if (cp->s_upcall) {
  842.       (*cp->s_upcall)(cp, oldstate, newstate);
  843.     }
  844.     try_send(cp, 1, cp->mode);
  845.     break;
  846.   case DISCONNECTING:
  847.     if (cp->peer) {
  848.       disc_ax25(cp->peer);
  849.       cp->peer = cp->peer->peer = NULLAX25;
  850.     }
  851.     if (cp->s_upcall) {
  852.       (*cp->s_upcall)(cp, oldstate, newstate);
  853.     }
  854.     send_packet(cp, DISC, POLL, NULLBUF);
  855.     break;
  856.   }
  857. }
  858.  
  859. int
  860. t1_timeout(struct ax25_cb *cp)
  861. {
  862.   if(!cp->dama) {
  863.     int32 tmp = (dur_timer(&cp->t1) * 5 + 2) / 4;
  864.  
  865.     if(tmp > T1_TIME * cp->iface->flags->t1init) {
  866.       tmp = T1_TIME * cp->iface->flags->t1init;
  867.     }
  868.     if(tmp < T1_TIME) {
  869.       tmp = T1_TIME;
  870.     }
  871.     set_timer(&cp->t1,tmp);
  872.     cp->cwind = 1;
  873.   }
  874.   cp->retries++;
  875.  
  876.   switch (cp->state) {
  877.   case DISCONNECTED:
  878.     break;
  879.   case CONNECTING:
  880.     if (cp->peer && cp->peer->state == DISCONNECTED) {
  881.       if (cp->retries > 2) {
  882.         cp->reason = LB_TIMEOUT;
  883.         setaxstate(cp, DISCONNECTED);
  884. #ifdef NETROM
  885.         nr_derate(cp);
  886. #endif
  887.       } else {
  888.         start_timer(&cp->t1);
  889.       }
  890.     } else if (cp->retries > cp->iface->flags->retries) {
  891.       cp->reason = LB_TIMEOUT;
  892.       setaxstate(cp, DISCONNECTED);
  893. #ifdef NETROM
  894.       nr_derate(cp);
  895. #endif
  896.     } else {
  897.       send_packet(cp, SABM, POLL, NULLBUF);
  898.     }
  899.     break;
  900.   case CONNECTED:
  901.     if(cp->retries > (cp->iface->flags->retries * 2)) {
  902.       cp->reason = LB_TIMEOUT;
  903.       setaxstate(cp, DISCONNECTED);
  904. #ifdef NETROM
  905.       nr_derate(cp);
  906. #endif
  907.     } else if((!cp->polling && !cp->remotebusy && cp->unack
  908.       && (len_p(cp->rxasm) - 1) <= cp->iface->flags->pthresh)
  909.       || cp->dama) {
  910.         struct mbuf *bp;
  911.         int old_vs = cp->vs;
  912.  
  913.         cp->vs = (cp->vs - cp->unack) & 7;
  914.         cp->sndtime[cp->vs] = 0;
  915.         dup_p(&bp, cp->rxasm, 0, MAXINT16);
  916.         send_packet(cp, I, CMD, bp);
  917.         cp->vs = old_vs;
  918.     } else {
  919.       send_ack(cp, POLL);
  920.     }
  921.     break;
  922.   case DISCONNECTING:
  923.     if (cp->retries > (cp->iface->flags->retries / 2)) {
  924.       cp->reason = LB_TIMEOUT;
  925.       setaxstate(cp, DISCONNECTED);
  926. #ifdef NETROM
  927.       nr_derate(cp);
  928. #endif
  929.     } else {
  930.       send_packet(cp, DISC, POLL, NULLBUF);
  931.     }
  932.     break;
  933.   }
  934.   return 0;
  935. }
  936.  
  937. static void
  938. t2_timeout(struct ax25_cb *cp)
  939. {
  940.   send_ack(cp, RESP);
  941. }
  942.  
  943. static void
  944. t3_timeout(struct ax25_cb *cp)
  945. {
  946.   if(cp->dama) {
  947.     cp->reason = LB_TIMEOUT;
  948.     setaxstate(cp,DISCONNECTED);
  949. #ifdef NETROM
  950.     nr_derate(cp);
  951. #endif
  952.     return;
  953.   }
  954.   if(!run_timer(&cp->t1) && cp->state == CONNECTED) {
  955.     if(cp->iface->flags->t3disc) {
  956.       disc_ax25(cp);
  957.     } else {
  958.       send_ack(cp, POLL);
  959.     }
  960.   }
  961. }
  962.  
  963. static void
  964. t4_timeout(struct ax25_cb *cp)
  965. {
  966.   if (!cp->polling) send_ack(cp, POLL);
  967. }
  968.  
  969. static void
  970. t5_timeout(struct ax25_cb *cp)
  971. {
  972.   try_send(cp, 1, STREAM);
  973. }
  974.  
  975. /* Look up entry in connection table */
  976. struct ax25_cb *
  977. find_ax25(char *remote,char *local)
  978. {
  979.     struct ax25_cb *axp, *axlast = NULLAX25;
  980.  
  981.     /* Search list */
  982.     for(axp = Ax25_cb; axp != NULLAX25; axlast = axp, axp = axp->next){
  983.         if(addreq(axp->path,remote) && addreq(axp->path + AXALEN,local)){
  984.             if(axlast != NULLAX25) {
  985.                 /* Move entry to top of the list */
  986.                 axlast->next = axp->next;
  987.                 axp->next = Ax25_cb;
  988.                 Ax25_cb = axp;
  989.             }
  990.             return axp;
  991.         }
  992.     }
  993.     return NULLAX25;
  994. }
  995.  
  996. static void near
  997. init_timer(struct ax25_cb *axp)
  998. {
  999.     if(axp->iface != NULLIF) {
  1000.         set_timer(&axp->t1,axp->iface->flags->t1init * 1000L);
  1001.         axp->t1.func = (void (*) __ARGS((void *))) t1_timeout;
  1002.         axp->t1.arg = axp;
  1003.  
  1004.         set_timer(&axp->t2,axp->iface->flags->t2init * 1000L);
  1005.         axp->t2.func = (void (*) __ARGS((void *))) t2_timeout;
  1006.         axp->t2.arg = axp;
  1007.  
  1008.         set_timer(&axp->t3,axp->iface->flags->t3init * 1000L);
  1009.         axp->t3.func = (void (*) __ARGS((void *))) t3_timeout;
  1010.         axp->t3.arg = axp;
  1011.  
  1012.         set_timer(&axp->t4,axp->iface->flags->t4init * 1000L);
  1013.         axp->t4.func = (void (*) __ARGS((void *))) t4_timeout;
  1014.         axp->t4.arg = axp;
  1015.  
  1016.         set_timer(&axp->t5,axp->iface->flags->t5init * 1000L);
  1017.         axp->t5.func = (void (*) __ARGS((void *))) t5_timeout;
  1018.         axp->t5.arg = axp;
  1019.     }
  1020. }
  1021.  
  1022. /* Create an ax25 control block. Allocate a new structure, if necessary,
  1023.  * and fill it with all the defaults. The caller
  1024.  * is still responsible for filling in the reply address
  1025.  */
  1026. static struct ax25_cb * near
  1027. cr_ax25(char *remote,char *local,struct iface *iface)
  1028. {
  1029.     struct ax25_cb *axp;
  1030.  
  1031.     if(remote == NULLCHAR)
  1032.         return NULLAX25;
  1033.  
  1034.     if((axp = find_ax25(remote,local)) == NULLAX25){
  1035.         /* Not already in table; create an entry */
  1036.         axp = mxallocw(sizeof(struct ax25_cb));
  1037.         axp->next = Ax25_cb;
  1038.         Ax25_cb = axp;
  1039.     }
  1040.     axp->state = DISCONNECTED;
  1041.     axp->cwind = 1;
  1042.     axp->user = -1;
  1043.     axp->iface = iface;
  1044.     init_timer(axp);
  1045.  
  1046.     /* Always to a receive and state upcall as default */
  1047.     /* Also bung in a default transmit upcall - in case */
  1048.     axp->r_upcall = s_arcall;
  1049.     axp->t_upcall = s_atcall;
  1050.     axp->s_upcall = s_ascall;
  1051.     axp->peer = NULLAX25;
  1052.     return axp;
  1053. }
  1054.  
  1055. /* Open an AX.25 connection */
  1056. struct ax25_cb *
  1057. open_ax25(
  1058. struct iface *iface,    /* Interface */
  1059. char *local,                    /* Local address */
  1060. char *remote,                   /* Remote address */
  1061. int mode,                               /* active/passive/server */
  1062. void (*r_upcall)(),             /* Receiver upcall handler */
  1063. void (*t_upcall)(),             /* Transmitter upcall handler */
  1064. void (*s_upcall)(),             /* State-change upcall handler */
  1065. int user)                               /* User linkage area */
  1066. {
  1067.     struct ax25_cb *axp = NULLAX25;
  1068.     char remtmp[AXALEN], path[3*AXALEN];
  1069.  
  1070.     if(remote == NULLCHAR){
  1071.         remote = remtmp;
  1072.         setcall(remote," ");
  1073.     }
  1074.     if((axp = find_ax25(remote,local)) != NULLAX25 && axp->state != DISCONNECTED)
  1075.         return NULLAX25;        /* Only one to a customer */
  1076.     if(axp == NULLAX25 && (axp = cr_ax25(remote,local,iface)) == NULLAX25)
  1077.         return NULLAX25;
  1078.  
  1079.     axp->r_upcall = r_upcall;
  1080.     axp->t_upcall = t_upcall;
  1081.     axp->s_upcall = s_upcall;
  1082.     axp->user = (user == -2) ? -1 : user;
  1083.  
  1084.     memcpy(path,remote,AXALEN);
  1085.     path[ALEN] &= ~E;
  1086.     memcpy(path + AXALEN,local,AXALEN);
  1087.     path[ALEN + AXALEN] |= E;
  1088.  
  1089.     switch(mode) {
  1090.     case AX_SERVER:
  1091.         axp->clone = 1;
  1092.     case AX_PASSIVE:        /* Note fall-thru */
  1093.         axp->state = LISTEN;
  1094.         return axp;
  1095.     case AX_ACTIVE:
  1096.         break;
  1097.     }
  1098.     build_path(axp,NULLIF,path,NEWSRT);
  1099.  
  1100.     if(!axp->iface) {
  1101.         del_ax25(axp);
  1102.         return NULLAX25;
  1103.     }
  1104.  
  1105.     if(user == -2) {
  1106.         addrcp(axp->path + AXALEN,axp->iface->hwaddr);
  1107.         axp->path[axp->pathlen - 1] |= E;
  1108.         axroute_add(axp,0);
  1109.     }
  1110.  
  1111.     switch(axp->state){
  1112.     case DISCONNECTED:
  1113.         if(user != -2)
  1114.             setaxstate(axp,CONNECTING);
  1115.         break;
  1116.     case CONNECTING:
  1117.         free_q(&axp->txq);
  1118.         free_q(&axp->rxasm);
  1119.         break;
  1120.     case DISCONNECTING:     /* Ignore */
  1121.         break;
  1122.     case CONNECTED:
  1123.         free_q(&axp->txq);
  1124.         setaxstate(axp,CONNECTING);
  1125.         break;
  1126.     }
  1127.     return axp;
  1128. }
  1129.  
  1130. int
  1131. send_ax25(struct ax25_cb *cp,struct mbuf *bp,int pid)
  1132. {
  1133.   int16 cnt;
  1134.  
  1135.   if (!(cp && bp)) {
  1136.     free_p(bp);
  1137.     return (-1);
  1138.   }
  1139.   cp->mode = pid;
  1140.   switch (cp->state) {
  1141.   case DISCONNECTED:
  1142.     free_p(bp);
  1143.     return (-1);
  1144.   case CONNECTING:
  1145.   case CONNECTED:
  1146.     if (!cp->closed) {
  1147.       if ((cnt = len_p(bp)) != 0) {
  1148.     if (pid == 0) {
  1149.           append(&cp->txq, bp);
  1150.     } else {
  1151.           enqueue(&cp->txq, bp);
  1152.     }
  1153.         cp->sndqtime = msclock();
  1154.  
  1155.     if(!cp->dama) {
  1156.       try_send(cp, 0, pid);
  1157.     }
  1158.       }
  1159.       return (int)cnt;
  1160.     }
  1161.   case DISCONNECTING:
  1162.     free_p(bp);
  1163.     return (-1);
  1164.   }
  1165.   return (-1);
  1166. }
  1167.  
  1168. /* Receive incoming data on an AX.25 connection */
  1169. struct mbuf *
  1170. recv_ax25(struct ax25_cb *axp,int16 cnt)
  1171. {
  1172.     struct mbuf *bp;
  1173.  
  1174.     if(!axp->rxq)
  1175.         return NULLBUF;
  1176.  
  1177.     if(cnt == 0 || axp->mode == DGRAM || axp->rcvcnt <= cnt){
  1178.         /* This means we want it all */
  1179.         bp = dequeue(&axp->rxq);
  1180.         cnt = len_p(bp);
  1181.         axp->rxq = NULLBUF;
  1182.     } else {
  1183.         bp = alloc_mbuf(cnt);
  1184.         bp->cnt = pullup(&axp->rxq,bp->data,cnt);
  1185.     }
  1186.     axp->rcvcnt -= cnt;
  1187.  
  1188.     /* If this has un-busied us, send a RR to reopen the window */
  1189.     if(axp->rnrsent && !busy(axp)) {
  1190.       send_ack(axp,RESP);
  1191.     }
  1192.     return bp;
  1193. }
  1194.  
  1195. /* Close an AX.25 connection */
  1196. int
  1197. disc_ax25(struct ax25_cb *axp)
  1198. {
  1199.     if(axp == NULLAX25 || axp->closed) {
  1200.         return -1;
  1201.     }
  1202.     axp->closed = 1;
  1203.     
  1204.     switch(axp->state){
  1205.     case LISTEN:
  1206.         del_ax25(axp);
  1207.     case DISCONNECTED:
  1208.     case DISCONNECTING:
  1209.         return -1;;
  1210.     case CONNECTED:
  1211.         if(!axp->txq && !axp->unack) {
  1212.             setaxstate(axp,DISCONNECTING);
  1213.         }
  1214.         return 0;
  1215.     case CONNECTING:
  1216.         setaxstate(axp, DISCONNECTED);
  1217.         return 0;
  1218.     }
  1219.     return -1;
  1220. }
  1221.  
  1222.  
  1223. /* Abruptly terminate an AX.25 connection */
  1224. int
  1225. reset_ax25(struct ax25_cb *axp)
  1226. {
  1227.     void (*upcall)();
  1228.  
  1229.     if (axp == NULLAX25) {
  1230.         return -1;
  1231.     }
  1232.     upcall = axp->s_upcall;
  1233.     axp->reason = LB_DM;
  1234.     setaxstate(axp,DISCONNECTED);
  1235.     
  1236.     /* Clean up if the standard upcall isn't in use */
  1237.     if(upcall != s_ascall) {
  1238.         del_ax25(axp);
  1239.     }
  1240.     return 0;
  1241. }
  1242.  
  1243. /* Remove entry from connection table */
  1244. void
  1245. del_ax25(struct ax25_cb *conn)
  1246. {
  1247.     struct ax25_cb *axp, *axlast = NULLAX25;
  1248.  
  1249.     for(axp = Ax25_cb; axp != NULLAX25; axlast = axp, axp = axp->next) {
  1250.         if(axp == conn) {
  1251.             break;
  1252.         }
  1253.     }
  1254.     if(axp == NULLAX25) {
  1255.         return; /* Not found */
  1256.     }
  1257.     if(axp->state != TIMEWAIT) {
  1258.         int i; 
  1259.         
  1260.         /* Timers should already be stopped, but just in case... */
  1261.         stop_timer(&axp->t1);
  1262.         stop_timer(&axp->t2);
  1263.         stop_timer(&axp->t3);
  1264.         stop_timer(&axp->t4);
  1265.         stop_timer(&axp->t5);
  1266.  
  1267.         /* Free allocated resources */
  1268.         for(i = 0; i < 8; i++) {
  1269.             free_p(axp->reseq[i].bp);
  1270.         }
  1271.         free_q(&axp->txq);
  1272.         free_q(&axp->rxasm);
  1273.         free_q(&axp->rxq);
  1274.         free_q(&axp->segasm);
  1275.         
  1276.         axp->state = TIMEWAIT;
  1277.         
  1278.         axp->t5.func = (void (*) __ARGS((void *))) del_ax25;
  1279.         axp->t5.arg = axp;
  1280.         set_timer(&axp->t5,30000);
  1281.         start_timer(&axp->t5);
  1282.         return;
  1283.     }    
  1284.     stop_timer(&axp->t5);
  1285.     
  1286.     /* Remove from list */
  1287.     if(axlast != NULLAX25) {
  1288.         axlast->next = axp->next;
  1289.     } else {
  1290.         Ax25_cb = axp->next;
  1291.     }
  1292.     xfree(axp);
  1293. }
  1294.  
  1295. /*
  1296.  * setcall - convert callsign plus substation ID of the form
  1297.  * "KA9Q-0" to AX.25 (shifted) address format
  1298.  *   Address extension bit is left clear
  1299.  *   Return -1 on error, 0 if OK
  1300.  */
  1301. int
  1302. setcall(char *out,char *call)
  1303. {
  1304.     int csize, i;
  1305.     unsigned ssid;
  1306.     char c, *dp;
  1307.  
  1308.     if(out == NULLCHAR || call == NULLCHAR || *call == '\0')
  1309.         return -1;
  1310.  
  1311.     /* Find dash, if any, separating callsign from ssid
  1312.      * Then compute length of callsign field and make sure
  1313.      * it isn't excessive
  1314.      */
  1315.     if((dp = strchr(call,'-')) == NULLCHAR)
  1316.         csize = strlen(call);
  1317.     else
  1318.         csize = (int)(dp - call);
  1319.  
  1320.     if(csize > ALEN)
  1321.         return -1;
  1322.  
  1323.     /* Now find and convert ssid, if any */
  1324.     if(dp != NULLCHAR) {
  1325.         dp++;   /* skip dash */
  1326.         ssid = atoi(dp);
  1327.         if(ssid > 15)
  1328.             return -1;
  1329.     } else
  1330.         ssid = 0;
  1331.  
  1332.     /* Copy upper-case callsign, left shifted one bit */
  1333.     for(i = 0; i < csize; i++) {
  1334.         c = *call++;
  1335.         if(islower(c))
  1336.             c = toupper(c);
  1337.         *out++ = c << 1;
  1338.     }
  1339.  
  1340.     /* Pad with shifted spaces if necessary */
  1341.     for(; i < ALEN; i++)
  1342.         *out++ = ' ' << 1;
  1343.  
  1344.     /* Insert substation ID field and set reserved bits */
  1345.     *out = 0x60 | (ssid << 1);
  1346.     *out |= E;
  1347.     return 0;
  1348. }
  1349.  
  1350. int
  1351. addreq(char *a,char *b)
  1352. {
  1353.     if (*a++ != *b++) return 0;
  1354.     if (*a++ != *b++) return 0;
  1355.     if (*a++ != *b++) return 0;
  1356.     if (*a++ != *b++) return 0;
  1357.     if (*a++ != *b++) return 0;
  1358.     if (*a++ != *b++) return 0;
  1359.     return (*a & SSID) == (*b & SSID);
  1360. }
  1361.  
  1362. void
  1363. addrcp(char *to,char *from)
  1364. {
  1365.     *to++ = *from++;
  1366.     *to++ = *from++;
  1367.     *to++ = *from++;
  1368.     *to++ = *from++;
  1369.     *to++ = *from++;
  1370.     *to++ = *from++;
  1371.     *to = (*from & SSID) | 0x60;
  1372. }
  1373.  
  1374. int
  1375. callreq(char *a)
  1376. {
  1377.     struct iface *ifp;
  1378.  
  1379.     for(ifp = Ifaces; ifp; ifp = ifp->next) {
  1380.         if(ifp->output == ax_output) {
  1381.             if(addreq(a,ifp->hwaddr)) {
  1382.                 return 1;
  1383.             }
  1384.         }
  1385.     }
  1386.     return 0;
  1387. }
  1388.  
  1389. /* Convert encoded AX.25 address to printable string */
  1390. char *
  1391. pax25(char *e,char *addr)
  1392. {
  1393.     int i;
  1394.     char c, *cp = e;
  1395.  
  1396.     for(i = ALEN; i != 0; i--) {
  1397.         c = (*addr++ >> 1) & 0x7f;
  1398.         if(c != ' ')
  1399.             *cp++ = c;
  1400.     }
  1401.     if ((*addr & SSID) != 0) {
  1402.         sprintf(cp,"-%d",(*addr >> 1) & 0xf);   /* ssid */
  1403.     } else {
  1404.         *cp = '\0';
  1405.     }
  1406.     return e;
  1407. }
  1408.  
  1409. static int near
  1410. put_reseq(struct ax25_cb *cp,struct mbuf *bp,int ns)
  1411. {
  1412.  
  1413.   char  *p;
  1414.   int  cnt, sum;
  1415.   struct axreseq *rp;
  1416.   struct mbuf *tp;
  1417.  
  1418.   if (next_seq(ns) == cp->vr) return 0;
  1419.  
  1420.   rp = &cp->reseq[ns];
  1421.   if (rp->bp) return 0;
  1422.   for (sum = 0, tp = bp; tp; tp = tp->next) {
  1423.     cnt = tp->cnt;
  1424.     p = tp->data;
  1425.     while (cnt--) sum += uchar(*p++);
  1426.   }
  1427.   if (ns != cp->vr && sum == rp->sum) return 0;
  1428.   rp->bp = bp;
  1429.   rp->sum = sum;
  1430.   return 1;
  1431. }
  1432.  
  1433. void
  1434. build_path(struct ax25_cb *cp,struct iface *ifp,char *newpath,int flags)
  1435. {
  1436.   char  buf[10*AXALEN];
  1437.   int  len, ndigi;
  1438.   char  *ap, *tp, *myaddr = 0;
  1439.   struct axroute_tab *rp = 0;
  1440.  
  1441.   /*** find address length and copy address into control block ***/
  1442.   for (ap = newpath; !(ap[ALEN] & E); ap += AXALEN) ;
  1443.   cp->pathlen = (int)(ap - newpath + AXALEN);
  1444.  
  1445.   if (flags & REVERSE) {
  1446.     addrcp(cp->path, newpath + AXALEN);
  1447.     addrcp(cp->path + AXALEN, newpath);
  1448.     for (tp = cp->path + 2 * AXALEN;
  1449.          tp < cp->path + cp->pathlen;
  1450.          tp += AXALEN, ap -= AXALEN) {
  1451.       addrcp(tp, ap);
  1452.     }
  1453.   } else {
  1454.     memcpy(cp->path, newpath, cp->pathlen);
  1455.   }
  1456.   /*** store iface pointer into control block ***/
  1457.   cp->iface = ifp;
  1458.  
  1459.   /*** save address for autorouting ***/
  1460.   if(cp->iface && (flags & REVERSE)) {
  1461.     /* NREX bke 920717 replace NR-Call with the Call of this iface */
  1462.     /*                 then add the route to the list and rename   */
  1463.     /*                 the target to the original call             */
  1464. #ifdef NETROM
  1465.     int k = ismyNcall(cp->path + AXALEN);
  1466.  
  1467.     if(k) {
  1468.       addrcp(cp->path + AXALEN, ifp->hwaddr);
  1469.     }
  1470. #endif
  1471.  
  1472.     axroute_add(cp,0);
  1473.  
  1474. #ifdef NETROM
  1475.     if(k) {
  1476.       addrcp(cp->path + AXALEN, Nrifaces[k-1].call);
  1477.     }
  1478. #endif
  1479. /* bke *  a bit confusing, isn't it? */
  1480.   }
  1481.   /*** find my digipeater address (use the last one) ***/
  1482.   for (ap = cp->path + 2 * AXALEN; ap < cp->path + cp->pathlen; ap += AXALEN) {
  1483.     if(!ifp) {
  1484.       for (ifp = Ifaces; ifp; ifp = ifp->next) {
  1485.         if (ifp->output == ax_output && addreq(ifp->hwaddr,ap)) {
  1486.           break;
  1487.         }
  1488.       }
  1489.     }
  1490.     if(addreq(ap,ifp->hwaddr)) {
  1491.       myaddr = ap;
  1492.     }
  1493.   }
  1494.   if(!(flags & REVERSE)) {
  1495.     /*** autorouting, remove all digipeaters before me ***/
  1496.     if (myaddr && myaddr > cp->path + 2 * AXALEN) {
  1497.       len = (int)((cp->path + cp->pathlen) - myaddr);
  1498.       memcpy(buf, myaddr, len);
  1499.       myaddr = cp->path + 2 * AXALEN;
  1500.       memcpy(myaddr, buf, len);
  1501.       cp->pathlen = 2 * AXALEN + len;
  1502.     }
  1503.     /*** add necessary digipeaters and find interface ***/
  1504.     ap = myaddr ? myaddr + AXALEN : cp->path + 2 * AXALEN;
  1505.     rp = axroute_tabptr(axptr((ap >= cp->path + cp->pathlen) ? cp->path : ap), 0);
  1506.     for (; rp; rp = rp->digi) {
  1507.       if (rp->digi && cp->pathlen < sizeof(cp->path)) {
  1508.         len = (int)((cp->path + cp->pathlen) - ap);
  1509.         if (len) {
  1510.           memcpy(buf, ap, len);
  1511.         }
  1512.     addrcp(ap, (char *) &rp->digi->call);
  1513.         if (len) {
  1514.           memcpy(ap + AXALEN, buf, len);
  1515.         }
  1516.         cp->pathlen += AXALEN;
  1517.       }
  1518.       cp->iface = rp->ifp;
  1519.     }
  1520.     if(!cp->iface) {
  1521.       cp->iface = axroute_default_ifp;
  1522.     }
  1523.     /*** replace my address with hwaddr of interface ***/
  1524.     if(myaddr != 0) {
  1525.         addrcp(cp->pathlen == 2 * AXALEN ? cp->path + AXALEN : cp->path + 2 * AXALEN,
  1526.           cp->iface ? cp->iface->hwaddr : Mycall);
  1527.     }
  1528.   }
  1529.   /*** clear all address bits ***/
  1530.   for (ap = cp->path; ap < cp->path + cp->pathlen; ap += AXALEN) {
  1531.     ap[ALEN] = (ap[ALEN] & SSID) | 0x60;
  1532.   }
  1533.   /*** set REPEATED bits for all digipeaters before and including me ***/
  1534.   if (myaddr) {
  1535.     for (ap = cp->path + 2 * AXALEN; ap <= myaddr; ap += AXALEN) {
  1536.       ap[ALEN] |= REPEATED;
  1537.     }
  1538.   }
  1539.   /*** mark end of address field ***/
  1540.   cp->path[cp->pathlen-1] |= E;
  1541.  
  1542.   if(flags & NEWSRT && !cp->dama) {
  1543.     init_timer(cp);
  1544.  
  1545.     /*** estimate round trip time ***/
  1546.     if (myaddr) {
  1547.       ndigi = (int)((cp->path + cp->pathlen - myaddr) / AXALEN) + 1;
  1548.     } else {
  1549.       ndigi = cp->pathlen / AXALEN;
  1550.     }
  1551.     cp->srt = (500 * cp->iface->flags->t1init * ndigi);
  1552.     cp->mdev = 0;
  1553.  
  1554.     reset_t1(cp);
  1555.   }
  1556. }
  1557.  
  1558. int
  1559. is_bud(char *icall,int store)
  1560. {
  1561.   struct btable_t {
  1562.     struct btable_t *next;
  1563.     char call[AXALEN];
  1564.   } ;
  1565.   static struct btable_t *btable, *p, **tp;
  1566.  
  1567.   /* this is necessary to ignore any ssid */
  1568.   char bcall[AXALEN];
  1569.   memcpy(bcall,icall,AXALEN);
  1570.   bcall[ALEN] = '0' << 1;
  1571.  
  1572.   tp = &btable;
  1573.  
  1574.   for (p = *tp; p && !addreq(p->call, bcall); p = p->next) ;
  1575.  
  1576.   if (!p && store) {
  1577.     p = mxallocw(sizeof(struct btable_t));
  1578.     addrcp(p->call,bcall);
  1579.     p->next = *tp;
  1580.     *tp = p;
  1581.   }
  1582.   return (int) (p != 0);
  1583. }
  1584.  
  1585. static int near
  1586. is_flexnet(char *call,int store)
  1587. {
  1588.   struct ftable_t {
  1589.     struct ftable_t *next;
  1590.     char call[AXALEN];
  1591.   };
  1592.   static struct ftable_t *ftable, *p, **tp;
  1593.  
  1594.   tp = &ftable;
  1595.  
  1596.   for (p = *tp; p && !addreq(p->call, call); p = p->next) ;
  1597.  
  1598.   if (!p && store) {
  1599.     p = mxallocw(sizeof(struct ftable_t));
  1600.     addrcp(p->call, call);
  1601.     p->next = *tp;
  1602.     *tp = p;
  1603.   }
  1604.   return (int) (p != 0);
  1605. }
  1606.  
  1607. static void near
  1608. axproto_recv(struct iface *ifp,struct mbuf *bp,char repeated)
  1609. {
  1610.   struct ax25_cb *cp, *cpp;
  1611.   int cmdrsp, control, for_me, type, pid, ipcam;
  1612.   char *cntrlptr,
  1613.     dama_flag = DAMA - (bp->data[AXALEN+ALEN] & DAMA);  /* DAMA bke 920531 */
  1614.  
  1615.   for(cntrlptr = bp->data + AXALEN;!(cntrlptr[ALEN] & E);cntrlptr += AXALEN) ;
  1616.  
  1617.   cntrlptr += AXALEN;
  1618.   control = uchar(*cntrlptr);
  1619.  
  1620.   if (control & 1) {
  1621.     if (control & 2) {
  1622.       type = control & ~PF;
  1623.     } else {
  1624.       type = control & 0xf;
  1625.     }
  1626.   } else {
  1627.     type = I;
  1628.   }
  1629.   for_me = addreq(bp->data,ifp->hwaddr);
  1630.  
  1631. #ifdef NETROM
  1632.   /* NREX bke 920716 Nr-Call ist mein Call... */
  1633.   if (!for_me && ismyNcall(bp->data)) {
  1634.     for_me = 1;
  1635.   }
  1636. #endif
  1637.   /* bke */
  1638.  
  1639.   if((cp = find_ax25(bp->data + AXALEN,bp->data)) != NULLAX25 && !for_me) {
  1640.     for_me = 2;
  1641.     if(type == SABM && repeated) {
  1642.       free_p(bp);
  1643.       return;
  1644.     }
  1645.   }
  1646.   if (!for_me && (type == UI || addreq(bp->data, bp->data + AXALEN)
  1647.     || is_flexnet(bp->data, 0) || is_flexnet(bp->data + AXALEN, 0))) {
  1648.       axroute(NULLAX25, bp);
  1649.       return;
  1650.   }
  1651.   if((pid = uchar(cntrlptr[1])) == PID_FLEXNET) {
  1652.     is_flexnet(bp->data + AXALEN, 1);       /* */
  1653.   }
  1654.   if ((bp->data[ALEN] & C) == (bp->data[ALEN + AXALEN] & C)) {
  1655.     cmdrsp = VERS1;
  1656.   } else {
  1657.     cmdrsp = ((bp->data[ALEN] & C) ? DST_C : SRC_C) | (control & PF);
  1658.   }
  1659.   if(cp) {
  1660.     if (for_me == 1 || (for_me == 2 && cp->peer != NULLAX25)) {
  1661.     /*  build_path(cp,ifp,bp->data,REVERSE);
  1662.     } else {*/
  1663.       char temp[AXALEN];
  1664.       addrcp(temp,bp->data);                        /* if the frame is not directly for */
  1665.       addrcp(bp->data,ifp->hwaddr);         /* us, but for a user of our gateway */
  1666.       build_path(cp,ifp,bp->data,REVERSE);  /* we MUST change the adress field to */
  1667.       addrcp(cp->path + AXALEN,temp);       /* to make axrouting work! */
  1668.       if(cp->pathlen == 2 * AXALEN) {       /* DB3FL.910110 */
  1669.         cp->path[AXALEN + ALEN] |= E;   /* set address field end bit */
  1670.       }
  1671.     }
  1672.     cpp = cp->peer;
  1673.   } else {
  1674.     cp = cr_ax25(bp->data + AXALEN,bp->data,ifp);
  1675.  
  1676.     build_path(cp, ifp, bp->data, REVERSE | NEWSRT);
  1677.  
  1678.     if (!for_me) {
  1679.       cp->peer = cpp = cr_ax25(bp->data,bp->data + AXALEN,NULLIF);
  1680.       cpp->peer = cp;
  1681.       build_path(cpp, NULLIF, bp->data, NEWSRT);
  1682.     } else {
  1683.       cpp = NULLAX25;
  1684.     }
  1685.   }
  1686.   if (type == SABM) {
  1687.     int  i;
  1688.     build_path(cp, ifp, bp->data, REVERSE);
  1689.     if (cp->unack) {
  1690.       start_timer(&cp->t1);
  1691.     } else {
  1692.       stop_timer(&cp->t1);
  1693.     }
  1694.     stop_timer(&cp->t2);
  1695.     stop_timer(&cp->t4);
  1696.     cp->polling = 0;
  1697.     cp->rnrsent = 0;
  1698.     cp->rejsent = 0;
  1699.     cp->remotebusy = 0;
  1700.     cp->vr = 0;
  1701.     cp->vs = cp->unack;
  1702.     cp->retries = 0;
  1703.     for (i = 0; i < 8; i++) {
  1704.       if (cp->reseq[i].bp) {
  1705.         free_q(&cp->reseq[i].bp);
  1706.         cp->reseq[i].bp = NULLBUF;
  1707.       }
  1708.     }
  1709.   }
  1710.   /* do it every time a frame is recveied to get defined timer values */
  1711.   if(dama_flag) {
  1712.     cp->dama = dama_flag;
  1713.     stop_timer(&cp->t1);
  1714.     stop_timer(&cp->t5);
  1715.     if(cp->state != CONNECTING && cp->state != DISCONNECTING) {
  1716.       set_timer(&cp->t1,0);
  1717.     }
  1718.     set_timer(&cp->t2,5000);
  1719.     set_timer(&cp->t5,0);
  1720.     start_timer(&cp->t3);
  1721.     cp->cwind = 7;
  1722.     cp->polling = 0;
  1723.     cp->retries = 0;
  1724.   }
  1725.   ipcam = 0;
  1726.  
  1727.   if (type == I) {
  1728.     /* IPCAM-feature - DB3FL.910104 */
  1729.     if((for_me == 1 || (for_me == 2 && cp->peer == NULLAX25))
  1730.       && pid == PID_NO_L3
  1731.       && uchar(cntrlptr[2]) == 0x45
  1732.       && uchar(cntrlptr[3]) == 0x00
  1733.       && uchar(cntrlptr[4]) < 0x02) {
  1734.         cntrlptr[1] = PID_IP;
  1735.         pid = PID_IP;
  1736.         ipcam = 1;
  1737.     }
  1738.     cp->mode = (pid == PID_NO_L3) ? STREAM : DGRAM;
  1739.     if(cpp) cpp->mode = cp->mode;
  1740.  
  1741.     if(pid == PID_IP) {
  1742.       struct arp_tab *arp;
  1743.  
  1744.       if ((arp = arp_add(           /* src_ipaddr */
  1745.         get32(&cntrlptr[14]),ARP_AX25,cp->path,0,1+ipcam)) != NULLARP) {
  1746.         stop_timer(&arp->timer);
  1747.         set_timer(&arp->timer,0L);
  1748.       }
  1749.     }
  1750.     start_timer(&cp->t3);
  1751.   }
  1752. /* */
  1753. //tprintf("state %d, name %s, forme %d \n",cp->state,cp->iface->name,for_me);
  1754.  
  1755.   switch (cp->state) {
  1756.   case DISCONNECTED:
  1757.     if (for_me == 1 || (for_me == 2 && cp->peer == NULLAX25)) {
  1758.       if (type == SABM && cmdrsp != VERS1 && cp->r_upcall) {
  1759.         send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1760.         setaxstate(cp, CONNECTED);
  1761.         start_timer(&cp->t3);
  1762.       } else {
  1763.         if (cmdrsp != RESP && cmdrsp != FINAL) {
  1764.           send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1765.         }
  1766.         del_ax25(cp);
  1767.       }
  1768.     } else {
  1769.       if (type == SABM && cmdrsp != VERS1 && cpp->state == DISCONNECTED) {
  1770.     setaxstate(cpp, CONNECTING);
  1771.       } else if (type == SABM && cmdrsp != VERS1 && cpp->state == CONNECTING) {
  1772.         build_path(cpp, NULLIF, bp->data, NEWSRT);
  1773.         send_packet(cpp, SABM, POLL, NULLBUF);
  1774.       } else {
  1775.         if (cmdrsp != RESP && cmdrsp != FINAL) {
  1776.           send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1777.         }
  1778.     if (cpp->state == DISCONNECTED) {
  1779.           del_ax25(cpp);
  1780.           del_ax25(cp);
  1781.     }
  1782.       }
  1783.     }
  1784.     break;
  1785.  
  1786.   case CONNECTING:
  1787.     switch (type) {
  1788.     case I:
  1789.     case RR:
  1790.     case RNR:
  1791.     case REJ:
  1792.       break;
  1793.     case SABM:
  1794.       if (cmdrsp != VERS1) {
  1795.         send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1796.     setaxstate(cp, CONNECTED);
  1797.       }
  1798.       break;
  1799.     case UA:
  1800.       if (cmdrsp != VERS1) {
  1801.         setaxstate(cp, CONNECTED);
  1802.         start_timer(&cp->t3);
  1803.       } else {
  1804.         if (cpp && cpp->state == DISCONNECTED) {
  1805.           send_packet(cpp, DM, FINAL, NULLBUF);
  1806.         }
  1807.         cp->reason = LB_DM;
  1808.     setaxstate(cp, DISCONNECTING);
  1809.       }
  1810.       break;
  1811.     case DISC:
  1812.       send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1813.     case DM:
  1814.     case FRMR:
  1815.       if (cpp && cpp->state == DISCONNECTED)
  1816.         send_packet(cpp, DM, FINAL, NULLBUF);
  1817.       cp->reason = LB_DM;
  1818.       setaxstate(cp, DISCONNECTED);
  1819. #ifdef NETROM
  1820.       nr_derate(cp);        /* TEST */
  1821. #endif
  1822.       break;
  1823.     }
  1824.     break;
  1825.  
  1826.   case CONNECTED:
  1827.     switch (type) {
  1828.     case I:
  1829.     case RR:
  1830.     case RNR:
  1831.     case REJ: {
  1832.       int nr = control >> 5;
  1833.  
  1834.       stop_timer(&cp->t1);
  1835.  
  1836.       if (((cp->vs - nr) & 7) < cp->unack) {
  1837.         if (!cp->polling && !cp->dama) {
  1838.           cp->retries = 0;
  1839.  
  1840.           if (cp->sndtime[(nr-1)&7]) {
  1841.             int32 rtt = msclock() - cp->sndtime[(nr-1)&7];
  1842.             int32 abserr = rtt > cp->srt ? rtt - cp->srt : cp->srt - rtt;
  1843.             cp->srt = ((AGAIN-1) * cp->srt + rtt + (AGAIN/2)) / AGAIN;
  1844.             cp->mdev = ((DGAIN-1) * cp->mdev + abserr + (DGAIN/2)) >> 3;
  1845.             reset_t1(cp);
  1846.           }
  1847.           cp->mdev += (cp->srt / cp->cwind) >> 3;
  1848.  
  1849.           if (cp->cwind < cp->iface->flags->maxframe) {
  1850.             cp->cwind++;
  1851.           } else {
  1852.             cp->cwind = cp->iface->flags->maxframe;
  1853.           }
  1854.     }
  1855.     while (((cp->vs - nr) & 7) < cp->unack) {
  1856.           cp->rxasm = free_p(cp->rxasm);
  1857.       cp->unack--;
  1858.     }
  1859.         if (cpp && cpp->rnrsent && !busy(cpp)) {
  1860.           send_ack(cpp, RESP);
  1861.         }
  1862.       }
  1863.       if (type == I) {
  1864.         int ns = (control >> 1) & 7;
  1865.  
  1866.         pullup(&bp, NULLCHAR, cntrlptr - bp->data + 1 + (cp->mode == STREAM));
  1867.  
  1868.         if (!bp) {
  1869.           bp = alloc_mbuf(0);
  1870.         }
  1871.         if (put_reseq(cp, bp, ns)) {
  1872.           int processed;
  1873.  
  1874.           while ((bp = cp->reseq[cp->vr].bp) != NULLBUF) {
  1875.             cp->reseq[cp->vr].bp = NULLBUF;
  1876.             cp->vr = next_seq(cp->vr);
  1877.             cp->rejsent = 0;
  1878.             processed = 0;
  1879.  
  1880.             if(for_me == 1 && uchar(cntrlptr[1]) != PID_NO_L3) {
  1881.               int seg;
  1882.  
  1883.               processed = 1;
  1884.               pid = PULLCHAR(&bp);
  1885.  
  1886.               if(cp->segremain != 0) {
  1887.                 /* Reassembly in progress; continue */
  1888.                 seg = PULLCHAR(&bp);
  1889.  
  1890.                 if(pid == PID_SEGMENT && (seg & SEG_REM) == cp->segremain - 1) {
  1891.                   /* Correct, in-order segment */
  1892.                   append(&cp->segasm,bp);
  1893.  
  1894.                   if((cp->segremain = (seg & SEG_REM)) == 0) {
  1895.                     /* Done; kick it upstairs */
  1896.                     bp = cp->segasm;
  1897.                     cp->segasm = NULLBUF;
  1898.                     cp->segremain = 0;
  1899.                     pid = PULLCHAR(&bp);
  1900.  
  1901.                     switch(pid) {
  1902.                     case PID_IP:
  1903.                       ip_route(ifp,ifp,bp,0);
  1904.                       break;
  1905. #ifdef AX25
  1906.                     case PID_ARP:
  1907.                       arp_input(ifp,bp);
  1908.                       break;
  1909. #endif
  1910. #ifdef NETROM
  1911.                     case PID_NETROM:
  1912.                       nr_route(bp,cp);
  1913.                       break;
  1914. #endif
  1915.                     }
  1916.                   }
  1917.                 } else {
  1918.                   /* Error! */
  1919.                   free_q(&cp->segasm);
  1920.                   cp->segasm = NULLBUF;
  1921.                   cp->segremain = 0;
  1922.                   free_p(bp);
  1923.                 }
  1924.               } else {
  1925.                 /* No reassembly in progress */
  1926.                 if(pid == PID_SEGMENT) {
  1927.                   /* Start reassembly */
  1928.                   seg = PULLCHAR(&bp);
  1929.  
  1930.                   if(!(seg & SEG_FIRST)) {
  1931.                     free_p(bp);     /* not first seg - error! */
  1932.                   } else {
  1933.                     /* Put first segment on list */
  1934.                     cp->segremain = seg & SEG_REM;
  1935.                     cp->segasm = bp;
  1936.                   }
  1937.                 } else {
  1938.                   /* Normal frame; send upstairs */
  1939.                   switch(pid) {
  1940.                   case PID_IP:
  1941.                     ip_route(ifp,ifp,bp,0);
  1942.                     break;
  1943. #ifdef AX25
  1944.                   case PID_ARP:
  1945.                     arp_input(ifp,bp);
  1946.                     break;
  1947. #endif
  1948. #ifdef NETROM
  1949.                   case PID_NETROM:
  1950.                     nr_route(bp,cp);
  1951.                     break;
  1952. #endif
  1953.                   }
  1954.                 }
  1955.               }
  1956.             }
  1957.             if(!processed) {
  1958.               if (for_me && cpp == NULLAX25) {
  1959.                 cp->rcvcnt += len_p(bp);
  1960.  
  1961.                 if(cp->user == 0 || Axi_sock == -1) {
  1962.                     free_p(bp);
  1963.                 } else {
  1964.                   if (cp->mode == STREAM) {
  1965.                     append(&cp->rxq, bp);
  1966.                   } else {
  1967.                     enqueue(&cp->rxq, bp);
  1968.                   }
  1969.                 }
  1970.               } else {
  1971.                 send_ax25(cpp, bp, cp->mode);
  1972.               }
  1973.             }
  1974.           }
  1975.         }
  1976.         if (cmdrsp == POLL) {
  1977.           if(cp->dama) {
  1978.             try_send(cp,1,cp->mode);
  1979.           }
  1980.           send_ack(cp, FINAL);
  1981.         } else {
  1982.           if(!cp->iface->flags->t2init) {
  1983.             send_ack(cp, RESP);
  1984.           } else {
  1985.             set_timer(&cp->t2,cp->iface->flags->t2init * 1000L);
  1986.             start_timer(&cp->t2);
  1987.           }
  1988.         }
  1989.         if (cp->r_upcall && cp->rcvcnt) {
  1990.           (*cp->r_upcall)(cp, cp->rcvcnt);
  1991.         }
  1992.       } else {
  1993.         if (cmdrsp == POLL) {
  1994.           if(cp->dama) {
  1995.             try_send(cp,1,cp->mode);
  1996.           }
  1997.           send_ack(cp, FINAL);
  1998.         }
  1999.         if (cp->polling && cmdrsp == FINAL) {
  2000.           cp->retries = cp->polling = 0;
  2001.         }
  2002.     if (type == RNR) {
  2003.           if (!cp->remotebusy) {
  2004.             cp->remotebusy = currtime;
  2005.           }
  2006.           set_timer(&cp->t4,cp->iface->flags->t4init * 1000L);
  2007.           start_timer(&cp->t4);
  2008.           cp->cwind = 1;
  2009.     } else {
  2010.           cp->remotebusy = 0;
  2011.           stop_timer(&cp->t4);
  2012.  
  2013.       if (cp->unack && type == REJ) {
  2014.             struct mbuf *bp1;
  2015.             int old_vs = cp->vs;
  2016.  
  2017.         cp->vs = (cp->vs - cp->unack) & 7;
  2018.         cp->sndtime[cp->vs] = 0;
  2019.             dup_p(&bp1, cp->rxasm, 0, MAXINT16);
  2020.             send_packet(cp, I, CMD, bp1);
  2021.             cp->vs = old_vs;
  2022.             cp->cwind = 1;
  2023.       } else if (cp->unack && cmdrsp == FINAL) {
  2024.             struct mbuf *bp1, *qp;
  2025.  
  2026.         cp->vs = (cp->vs - cp->unack) & 7;
  2027.             for (qp = cp->rxasm; qp; qp = qp->anext) {
  2028.           cp->sndtime[cp->vs] = 0;
  2029.               dup_p(&bp1, qp, 0, MAXINT16);
  2030.               send_packet(cp, I, CMD, bp1);
  2031.         }
  2032.       }
  2033.     }
  2034.       }
  2035.       try_send(cp, 1, cp->mode);
  2036.  
  2037.       if (cp->polling || cp->unack && !cp->remotebusy) {
  2038.         start_timer(&cp->t1);
  2039.       }
  2040.       if (cp->closed && !cp->txq && !cp->unack
  2041.        || cp->remotebusy && cp->remotebusy + 900l < currtime) {
  2042.         setaxstate(cp, DISCONNECTING);
  2043.       }
  2044.       break;
  2045.     }
  2046.     case SABM:
  2047.       send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2048.       try_send(cp, 1, cp->mode);
  2049.       break;
  2050.     case DISC:
  2051.       send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2052.     case DM:
  2053.       setaxstate(cp, DISCONNECTED);
  2054.       break;
  2055.     case UA:
  2056.       cp->remotebusy = 0;
  2057.       stop_timer(&cp->t4);
  2058.       if (cp->unack) {
  2059.         start_timer(&cp->t1);
  2060.       }
  2061.       try_send(cp, 1, cp->mode);
  2062.       break;
  2063.     case FRMR:
  2064.       setaxstate(cp, DISCONNECTING);
  2065.       break;
  2066.     }
  2067.     break;
  2068.  
  2069.   case DISCONNECTING:
  2070.     switch (type) {
  2071.     case I:
  2072.     case RR:
  2073.     case RNR:
  2074.     case REJ:
  2075.     case SABM:
  2076.       if (cmdrsp != RESP && cmdrsp != FINAL) {
  2077.         send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2078.       }
  2079.       break;
  2080.     case DISC:
  2081.       send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2082.     case DM:
  2083.     case UA:
  2084.     case FRMR:
  2085.       setaxstate(cp, DISCONNECTED);
  2086.       break;
  2087.     }
  2088.     break;
  2089.   }
  2090.   free_p(bp);
  2091.   return;
  2092. }
  2093.  
  2094. #ifdef AXIP
  2095. static int
  2096. axip_stop(struct iface *iface,int tmp)
  2097. {
  2098.      axipaddr[iface->dev] = 0;
  2099.      return 0;
  2100. }
  2101.  
  2102. /* attach a fake AX.25 interface for AX.25 on top of IP */
  2103. /* argv[0] == "axip"
  2104.  * argv[1] == name of new interface
  2105.  * argv[2] == MTU
  2106.  * argv[3] == hostname of remote end of wormhole
  2107.  * argv[4] == optional callsign is not necessary when using WNOS
  2108.  */
  2109. int
  2110. axip_attach(int argc,char **argv,void *p)
  2111. {
  2112.      int i;
  2113.      struct iface *ifp;
  2114.  
  2115.      if(if_lookup(argv[1]) != NULLIF) {
  2116.         tprintf(Ifexist,argv[1]);
  2117.         return -1;
  2118.      }
  2119.      for(i = 0; i < NAX25; ++i) {
  2120.         if(axipaddr[i] == 0) {
  2121.             break;
  2122.         }
  2123.      }
  2124.      if(i == NAX25) {
  2125.         tprintf("Max %d AXIP ifaces\n",NAX25);
  2126.         return -1;
  2127.      }
  2128.      if((axipaddr[i] = resolve(argv[3])) == 0) {
  2129.         tprintf(Badhost,argv[3]);
  2130.         return -1;
  2131.      }
  2132.      ifp = mxallocw(sizeof(struct iface));
  2133.      ifp->dev = i;
  2134.      ifp->addr = Ip_addr;
  2135.      ifp->name = strxdup(argv[1]);
  2136.      ifp->mtu = atoi(argv[2]);
  2137.      setencap(ifp,"AX25");
  2138.      ifp->hwaddr = strxdup(Mycall);
  2139.      ifp->raw = axip_raw;
  2140.      ifp->stop = axip_stop;
  2141.      init_maxheard(ifp);
  2142.      init_flags(ifp);
  2143.      ifp->niface = Niface++;
  2144.      ifp->next = Ifaces;
  2145.      Ifaces = ifp;
  2146.      return 0;
  2147. }
  2148. #endif /* AXIP */
  2149.  
  2150. /* AX25 log functions */
  2151. void
  2152. init_maxheard(struct iface *ifp)
  2153. {
  2154.     ifp->Hmax = MAXDEFAULT;
  2155.     ifp->Hcurrent = 0;
  2156.     ifp->lq = cxallocw(MAXDEFAULT,sizeof(struct lq));
  2157. }
  2158.  
  2159. int
  2160. maxheard(struct iface *ifp,int num)
  2161. {
  2162. struct lq *lnew = cxallocw(num,sizeof(struct lq)), *lold = ifp->lq;
  2163.  
  2164.    semwait(&MHwait,1);                  /* request/wait                 */
  2165.    ifp->lq = lnew;
  2166.    ifp->Hcurrent = 0;
  2167.    ifp->Hmax = num;
  2168.    semrel(&MHwait);
  2169.    xfree(lold);
  2170.    return(0);
  2171. }
  2172.  
  2173. static struct lq * near
  2174. al_lookup(struct iface *ifp,char *addr)
  2175. {
  2176. int i;
  2177. struct lq *lp = ifp->lq;
  2178.  
  2179.    semwait(&MHwait,0);
  2180.  
  2181.    for(i = 0; i < ifp->Hmax; i++) {
  2182.       if(addreq(lp->addr,addr)) {
  2183.      return lp;
  2184.       }
  2185.       if (lp->time == 0)
  2186.      break;
  2187.       lp++;
  2188.    }
  2189.    return NULLLQ;
  2190. }
  2191.  
  2192. /*----------------------------------------------------------------------*
  2193. * Create a new entry in the AX.25 link table                            *
  2194. *-----------------------------------------------------------------------*/
  2195. static struct lq * near
  2196. al_create(struct iface *ifp)
  2197. {
  2198. struct lq *lp, *lo = NULLLQ;
  2199. int32 time = currtime;
  2200.  
  2201.    semwait(&MHwait,1);
  2202.  
  2203.    if (ifp->Hcurrent < ifp->Hmax)   {
  2204.       lp = &ifp->lq[ifp->Hcurrent];
  2205.       ifp->Hcurrent++;
  2206.    } else   {                           /* look for the oldest one      */
  2207.       int i;
  2208.  
  2209.       lp = ifp->lq;
  2210.       for (i = 0; i < ifp->Hcurrent; i++)   {
  2211.      if (lp->time < time)  {
  2212.         time = lp->time;
  2213.         lo = lp;
  2214.      }
  2215.      lp++;
  2216.       }
  2217.       lp = lo;
  2218.    }
  2219.    semrel(&MHwait);
  2220.    return lp;
  2221. }
  2222.  
  2223. /*----------------------------------------------------------------------*
  2224. * Log the address of an incoming packet                                 *
  2225. *-----------------------------------------------------------------------*/
  2226. static void near
  2227. logaddr(struct iface *ifp,char *addr,char pid)
  2228. {
  2229.    struct lq *lp;
  2230.  
  2231.    if(MHwait || addreq(addr,ifp->hwaddr)) {
  2232.       /* Don't log our own packets if we hear them */
  2233.       return;
  2234.    }
  2235.    if((lp = al_lookup(ifp,addr)) == NULLLQ &&
  2236.     (lp = al_create(ifp)) == NULLLQ) {
  2237.       return;
  2238.    }
  2239.    memcpy(lp->addr,addr,AXALEN);
  2240.    lp->pid = pid;
  2241.    lp->currxcnt++;
  2242.    lp->time = currtime;
  2243. }
  2244.  
  2245. /*----------------------------------------------------------------------*
  2246. *-----------------------------------------------------------------------*/
  2247. int
  2248. axflush(struct iface *ifp)
  2249. {
  2250.    semwait(&MHwait,1);
  2251.    ifp->rawsndcnt = ifp->Hcurrent = 0;
  2252.    memset(ifp->lq,0,ifp->Hmax * sizeof(struct lq));  /* brute force */
  2253.    semrel(&MHwait);
  2254.    return 0;
  2255. }
  2256.  
  2257. #endif
  2258.